local extension = Package("freak_alliance_warrior")
extension.extensionName = "freak_alliance"

Fk:loadTranslationTable{
  ["freak_alliance"] = "怀异集", 
  ["freak_alliance_warrior"] = "怀异集·虎贲",
  ["fa"] = "怀异",
}

local U = require "packages/utility/utility"

local fa__dingfeng = General(extension, "fa__dingfeng", "wu", 4,4)

local fa__xianjing = fk.CreateTriggerSkill{
  name = "fa__xianjing",
  anim_type = "offensive",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Judge
  end,
  on_use = function(self, event, target, player, data)
      local room = player.room
      local judge = {
        who = player,
        reason = self.name,
        pattern = ".",
      }
      room:judge(judge)
      local choice = room:askForChoice(player, {"fa__xianjing1", "fa__xianjing2"}, self.name)
      if choice == "fa__xianjing2" then
      if judge.card.suit ~= Card.Club then
        player:skip(Player.Draw)
      end
      player:broadcastSkillInvoke("fa__xianjing", 2)
      room:setPlayerMark(target, "@@fa__xianjing_disresponsive", 1)
        U.askForUseVirtualCard(room, player, "duel", nil, self.name, nil, false)
      room:setPlayerMark(target, "@@fa__xianjing_disresponsive", 0)
    else if choice == "fa__xianjing1" then
      if judge.card.suit == Card.Spade and judge.card.number > 1 and judge.card.number < 10 then
        room:damage{
          to = player,
          damage = 3,
          damageType = fk.ThunderDamage,
          skillName = self.name,
        }
      end
      player:broadcastSkillInvoke("fa__xianjing", 1)
        U.askForUseVirtualCard(room, player, "duel", nil, self.name, nil, false)
      end
    end
  end,
}
local fa__xianjing_disresponsive = fk.CreateTriggerSkill{
  name = "#fa__xianjing_disresponsive",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    if target == player and data.card and table.contains(data.card.skillNames, "fa__xianjing") then
        return data.card.trueName == "duel" and player:getMark("@@fa__xianjing_disresponsive") ~= 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
      data.disresponsiveList = table.map(room.alive_players, Util.IdMapper)
  end,
}

fa__xianjing:addRelatedSkill(fa__xianjing_disresponsive)
fa__dingfeng:addSkill(fa__xianjing)

Fk:loadTranslationTable{
  ["fa__dingfeng"] = "丁奉", 
  ["#fa__dingfeng"] = "寸锋尺祚",
  ["cv:fa__dingfeng"] = "天外之音",
  ["illustrator:fa__dingfeng"] = "魔鬼鱼",
  ["designer:fa__dingfeng"] = "折翼",

  ["fa__xianjing"] = "险径",
  [":fa__xianjing"] = "判定阶段开始时，你可以进行判定；判定后，你可以选择一项：令此判定为【闪电】判定并视为使用【决斗】；或令此判定为【兵粮寸断】判定并视为使用不可响应的【决斗】。",
  ["#fa__xianjing-active"] = "发动 险径，可能承受【闪电】或【兵粮寸断】负面效果，并视为使用【决斗】%arg",
  ["#fa__xianjing-ask"] = "险径：你可选择一名其他角色，视为对其使用一张【决斗】",
  ["fa__xianjing1"] = "令此判定为【闪电】判定并视为使用【决斗】",
  ["fa__xianjing2"] = "令此判定为【兵粮寸断】判定并视为使用不可响应的【决斗】",
  ["@@fa__xianjing_disresponsive"] = "险径 不可响应",


  ["$fa__xianjing1"] = "欺君叛国之贼，当受白刃之罚！",
  ["$fa__xianjing2"] = "天公不作美？出奇建功回！",
  ["$fa__xianjing3"] = "（咳咳）此番劳师无果，恐为主公所忌...",
  ["~fa__dingfeng"] = "既无功，难受禄...",
}

local fa__madai = General(extension, "fa__madai", "shu", 4,4)

local fa__qianxi = fk.CreateTriggerSkill{
  name = "fa__qianxi",
  anim_type = "offensive",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target.phase == Player.Start and player:hasSkill(self) and (target == player or player:distanceTo(target) == 1)
  end,
  on_cost = function(self, event, target, player, data)
    local card = player.room:askForCard(player, 1, 1, true, self.name, true, "slash", "#fa__qianxi-card")
    if #card > 0 then
      self.cost_data = card
      return true
      end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:recastCard(self.cost_data, player, self.name)
    room:setPlayerMark(target, "@@fa__qianxi-turn",1)
  end,
}

local fa__qianxi_trigger= fk.CreateTriggerSkill{
  name = "#fa__qianxi_trigger",
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target.phase ~= Player.NotActive and not player:isNude() 
    and target:getMark("@@fa__qianxi-turn") ~= 0 then
      local room = player.room
      local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
      if use_event == nil then return false end
      local mark = U.getMark(target, "fa__qianxi_record-turn")
      local burst = #table.filter(player:getCardIds("h"), function(id) return Fk:getCardById(id).type == Card.TypeBasic end)
      if table.contains(mark, use_event.id) then
        return #mark > 0 and mark[burst] == use_event.id
      end
      mark = {}
      room.logic:getEventsOfScope(GameEvent.UseCard, burst, function(e)
        local use = e.data[1]
        if use.from == target.id then
          table.insert(mark, e.id)
          return true
        end
        return false
      end, Player.HistoryTurn)
      room:setPlayerMark(target, "fa__qianxi_record-turn", mark)
      return #mark > 0 and mark[burst] == use_event.id
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:showCards(player:getCardIds("h"))
    room:damage{
      from = player,
      to = target,
      damage = 1,
      skillName = "fa__qianxi",
    }
  end,
}

fa__madai:addSkill("mashu")
fa__qianxi:addRelatedSkill(fa__qianxi_trigger)
fa__madai:addSkill(fa__qianxi)

Fk:loadTranslationTable{
  ["fa__madai"] = "马岱", 
  ["#fa__madai"] = "骋疆袭爵",
  ["cv:fa__madai"] = "天外之音",
  ["illustrator:fa__madai"] = "第七个桔子",
  ["designer:fa__madai"] = "绯瞳",

  ["fa__qianxi"] = "潜袭",
  [":fa__qianxi"] = "你距离1以内角色的准备阶段，你可以重铸一张【杀】，其本回合使用第X张牌后，你可以展示手牌并对其造成1点伤害。（X为你手牌中的基本牌数）",
  ["#fa__qianxi-card"] = "发动 潜袭，请重铸一张【杀】，令当前回合角色被 “潜袭” ",
  ["@@fa__qianxi-turn"] = "已被潜袭",
  ["#fa__qianxi_trigger"] = "潜袭",

  ["$fa__qianxi1"] = "岱，愿从文长将军北伐，成千秋万载之功业！",
  ["$fa__qianxi2"] = "（命中）哼，丞相临终口谕于我，何谓不敢杀你！",
  ["$fa__qianxi3"] = "（未命中）文长所行并无失当，莫非杨威公构陷之？",
  ["~fa__madai"] = "虽袭兄长之爵，只恨功业难觅...",
}

return extension